package com.ksd.modules.fileRecord.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.convert.PageInfoUtil;
import com.ksd.modules.fileRecord.entity.SysFileRecord;
import com.ksd.modules.fileRecord.mapper.SysFileRecordMapper;
import com.ksd.modules.fileRecord.po.SysFileRecordSavePo;
import com.ksd.modules.fileRecord.po.SysFileRecordSearchPo;
import com.ksd.modules.fileRecord.service.ISysFileRecordService;
import com.ksd.modules.fileRecord.vo.SysFileRecordVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 文件上传记录表 服务实现类
 * </p>
 *
 * @author Auto Generator
 * @since 2023-09-17
 */
@Service
public class SysFileRecordService extends BaseServiceImpl<SysFileRecordMapper, SysFileRecord> implements ISysFileRecordService {

    public Page<SysFileRecordVo> getList(@RequestBody SysFileRecordSearchPo param) {
        LambdaQueryWrapper<SysFileRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(param.getIdentifier()), SysFileRecord::getIdentifier, param.getIdentifier());
        lambdaQueryWrapper.eq(param.getFolderId() != null, SysFileRecord::getFolderId, param.getFolderId());
        lambdaQueryWrapper.eq(param.getType() != null, SysFileRecord::getType, param.getType());
        if (StringUtils.isNotEmpty(param.getKeyword())) {
            lambdaQueryWrapper.and(c -> c.like(SysFileRecord::getOriginalName, param.getKeyword())
                    .or().like(SysFileRecord::getRemark, param.getKeyword()));
        }
        lambdaQueryWrapper.orderByDesc(SysFileRecord::getCreateTime);
        Page<SysFileRecord> page = this.page(new Page<>(param.getPageNo(), param.getPageSize()), lambdaQueryWrapper);
        Page<SysFileRecordVo> sysFileRecordVoPage = PageInfoUtil.pageInfo2VO(page, SysFileRecordVo.class);
        return sysFileRecordVoPage;
    }
    @Override
    public List<Map<String, Object>> statistics() {
        List<Map<String, Object>> statistics = this.baseMapper.statistics();
        return statistics.stream()
                .filter(map -> {
                    int key = (int) map.get("type");
                    if (key == 1) {
                        map.put("title", "图片");
                        return true;
                    } else if (key == 2) {
                        map.put("title", "视频");
                        return true;
                    }
                    return false;
                })
                .collect(Collectors.toList());

    }
    @Override
    public boolean saveUpdate(SysFileRecordSavePo platformDownloadSavePO) {
        return this.updateById(BeanUtil.copyProperties(platformDownloadSavePO, SysFileRecord.class));
    }
}
